package com.caigou.modules.personal.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.personal.bean.FamousItemBean
import com.caigou.modules.personal.bean.InfoBean
import com.caigou.modules.personal.bean.PostBean
import com.caigou.modules.personal.bean.UserPageInfoBean
import com.caigou.modules.personal.repository.PersonalRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/12/06
 */
@HiltViewModel
class PersonalPageViewModel @Inject constructor(private val repository: PersonalRepository) : ViewModel() {

    val userPageInfoLiveData = MutableLiveData<Resource<UserPageInfoBean>>()
    val ticketListLiveData = MutableLiveData<Resource<PostBean>>()
    val famousListLiveData = MutableLiveData<Resource<MutableList<FamousItemBean>>>()
    val infoListLiveData = MutableLiveData<Resource<InfoBean>>()

    fun getUserPageInfo(userId: String, currentUserId: String = "") {
        userPageInfoLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getUserPageInfo(userId, currentUserId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<UserPageInfoBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    userPageInfoLiveData.value = it
                }
        }
    }

    fun getTicketList(type: Int, userId: String) {
        ticketListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getTicketList(type, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<PostBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    ticketListLiveData.value = it
                }
        }
    }

    fun getFamousList(type: Int, userId: String) {
        famousListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getFamousList(type, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<MutableList<FamousItemBean>>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    famousListLiveData.value = it
                }
        }
    }

    fun getInfoList(page: Int, limit: Int, userId: String) {
        infoListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getInfoList(page, limit, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<InfoBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    infoListLiveData.value = it
                }
        }
    }
}